#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
test.py - main.py 的单元测试


"""

import unittest
import os
from main import (
    count_characters,
    count_words,
    count_sentences,
    count_lines,
    count_empty_lines,
    count_comment_lines
)

class TestWC(unittest.TestCase):
    """测试 main.py 的各个功能函数"""

    def setUp(self):
        """设置测试数据 —— 从 textexample.txt 文件中读取真实内容"""
        # 确保文件存在
        if not os.path.exists('textexample.txt'):
            raise FileNotFoundError("请确保 textexample.txt 文件存在于项目根目录")

        with open('textexample.txt', 'r', encoding='utf-8') as f:
            self.test_text = f.read()

        # 边界情况测试用例
        self.empty_text = ""
        self.only_spaces = "   \n\n  \t  \n"

        # 注释行测试用例
        self.comment_text = """# Comment 1
print("Hello")
// Comment 2
x = 5
# Comment 3"""

    def test_count_characters(self):
        """测试字符数统计"""
        result = count_characters(self.test_text)
        print(f"实际字符数: {result}")  # 调试输出

        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_count_words(self):
        """测试单词数统计"""
        result = count_words(self.test_text)
        print(f"实际单词数: {result}")
        # self.assertEqual(result, 286)
        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_count_sentences(self):
        """测试句子数统计"""
        result = count_sentences(self.test_text)
        print(f"实际句子数: {result}")
        # self.assertEqual(result, 9)
        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_count_lines(self):
        """测试总行数统计"""
        result = count_lines(self.test_text)
        print(f"实际总行数: {result}")
        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_count_empty_lines(self):
        """测试空行数统计"""
        result = count_empty_lines(self.test_text)
        print(f"实际空行数: {result}")
        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_count_comment_lines(self):
        """测试注释行数统计"""
        result = count_comment_lines(self.test_text)
        print(f"实际注释行数: {result}")
        self.assertIsInstance(result, int)
        self.assertGreaterEqual(result, 0)

    def test_comment_lines_with_custom_chars(self):
        """测试自定义注释符"""
        text = """/* This is a C-style comment */
print("Hello")
-- Lua comment
x = 5"""
        # 自定义注释符
        result = count_comment_lines(text, ['/*', '--'])
        self.assertEqual(result, 2)

    def test_edge_cases(self):
        """测试边界情况"""
        # 只有标点符号
        self.assertEqual(count_words("!!! ??? ..."), 0)
        # 只有数字
        self.assertEqual(count_words("123 456 789"), 3)
        # 混合情况
        self.assertEqual(count_words("Hello123 world! 456"), 3)

if __name__ == '__main__':
    unittest.main(verbosity=2)